package com.oot.twitter;

import java.util.ArrayList;
import java.util.List;

import android.content.SharedPreferences;
import android.view.Window;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.oot.twitter.info.User;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class AutoLogin extends Activity {

	private String username = "l";
	private String password = "l";
	private String user_id  = "1";
	
	private Class targetClass = Twitter.class;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!autoLogin(getCookie()))
            login();
    }

    private String getCookie() {
        // get cookie for default user
        SharedPreferences settings = getSharedPreferences(GlobalData.COKKIE_FILE, MODE_PRIVATE);
        String default_user = settings.getString(GlobalData.DEFAULT_USER, "");
        return settings.getString(default_user, "");

    }

    private boolean autoLogin(String cookieString) {
        if (cookieString.equals("")) return false;
        BasicCookieStore cookieStore = new BasicCookieStore();
        BasicClientCookie cookie     = new BasicClientCookie("PHPSESSID", "name");
        cookie.setDomain(ConnectionTwitter.DOMAIN_NAME);
        cookie.setValue(cookieString);
        cookie.setVersion(0);
        cookie.setPath("/");

        cookieStore.addCookie(cookie);

        // set cookie to connection
        GlobalData.client.setCookieStore(cookieStore);
        System.out.println("Cookie String :" + cookieStore.toString());
        GlobalData.currentUser =new User(ConnectionTwitter.sendRequestJSon(ConnectionTwitter.AUTOLOGIN_URL, new ArrayList<NameValuePair>()));
        if (GlobalData.currentUser == null) return false;

        // goto target class
        Intent target = new Intent(AutoLogin.this, targetClass);
        startActivity(target);
        return true;
    }

    private void login() {
    	List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    	nameValuePairs.add(new BasicNameValuePair("username", username));
    	nameValuePairs.add(new BasicNameValuePair("password", password));
    	
    	JSONObject json = ConnectionTwitter.sendRequestJSon(ConnectionTwitter.LOGIN_URL, nameValuePairs);
    	try{
	    	if (!json.getString("result").equals("1")){
	    		System.out.println("Login fail");
	    		return;
	    	}
	    	
	    	GlobalData.currentUser = new User(json.getJSONObject("user"));
    	}catch(Exception e){
    		System.out.println("Error parsing json when login");
    		return;
    	}


        // Get Cookie
        CookieStore cookieStore = ((DefaultHttpClient)GlobalData.client).getCookieStore();
        String value = ((BasicClientCookie)cookieStore.getCookies().get(0)).getValue();

        // Save cookie to settings
        SharedPreferences settings = getSharedPreferences(GlobalData.COKKIE_FILE, MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(GlobalData.currentUser.getUser_id(), value);
        editor.putString(GlobalData.DEFAULT_USER, GlobalData.currentUser.getUser_id());
        editor.commit();

        System.out.println("Normal login : "+ cookieStore.toString());
        System.out.println("Cookie receive"+ value);

        if (true) return;


    	Intent target = new Intent(AutoLogin.this, targetClass);
    	startActivity(target);
    }
    
}
